package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.nursingProjectPlanDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProject;

import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.nursingProjectPlanVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;
    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    /**
     * 获取所有护理计划
     * @return
     */
    @Override
    public List<NursingPlanVo> all() {
        List<NursingPlanVo> nursingPlanVo = nursingPlanMapper.all();
        return nursingPlanVo;
    }

    /**
     * 添加护理计划
     * @param nursingPlanDto
     */
    @Override
    @Transactional
    public void add(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);
        nursingPlanMapper.add(nursingPlan);
        log.info("02新增员工ID" + nursingPlan.getId());

        nursingPlan.getProjectPlans().forEach(nursingProjectPlan -> nursingProjectPlan.setPlanId(nursingPlan.getId()));

        if (nursingPlan.getProjectPlans() != null && nursingPlan.getProjectPlans().size() > 0 ){
            nursingProjectPlanMapper.insertBatch(nursingPlan.getProjectPlans());
        }


    }

    /**
     * 分页条件查询护理计划
     * @param name
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    @Override
    public PageResponse<NursingPlanVo> getByPage(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        List<NursingPlanVo> nursingPlanVo = nursingPlanMapper.getByPage(name, status);
        nursingPlanVo.forEach(np -> {
            List<NursingLevel> nursingLevels = nursingLevelMapper.seleteById(np.getId());
            if (nursingLevels != null){
                np.setCount(1);
            }else {
                np.setCount(0);
            }
        });
        Page<NursingPlanVo> page = (Page<NursingPlanVo>) nursingPlanVo;
                PageResponse<NursingPlanVo> nursingPlanVoPageResponse = new PageResponse<>(page);
        // nursingPlanVoPageResponse.setTotal(page.getTotal());
        // nursingPlanVoPageResponse.setPageSize(page.getPageSize());
        // nursingPlanVoPageResponse.setPages((long) page.getPages());
        // nursingPlanVoPageResponse.setPage(page.getPageNum());

        nursingPlanVoPageResponse.setRecords(page.getResult());
        return nursingPlanVoPageResponse;
    }

    /**
     * 根据id查询护理计划
     * @param id
     * @return
     */
    @Override
    public NursingPlanVo getById(Long id) {
        NursingPlanVo nursingPlanVo = nursingPlanMapper.getById(id);
        List<nursingProjectPlanVo> projectPlans = nursingPlanMapper.getpjp(id);
        // projectPlans.forEach(nursingProjectPlan -> {
        //     // nursingProjectPlan.getProjectId();
        //     NursingProject nursingProject = nursingProjectMapper.selectById(nursingProjectPlan.getProjectId());
        //     nursingProjectPlan.setProjectName(nursingProject.getName());
        // });
        projectPlans.forEach(nursingProjectPlanVo -> {
            NursingProject nursingProject = nursingProjectMapper.selectById(nursingProjectPlanVo.getProjectId());
            // nursingProjectPlanVo nursingProjectPlanVo = new nursingProjectPlanVo();
            nursingProjectPlanVo.setProjectName(nursingProject.getName());
        });

        nursingPlanVo.setProjectPlans(projectPlans);
        return nursingPlanVo;
    }

    /**
     * 编辑更新护理计划
     * @param nursingPlanDto
     */
    @Override
    public void update(NursingPlanDto nursingPlanDto) {
        // //删除需要更新的护理计划
        // nursingPlanMapper.delete(nursingPlanDto);
        // //重新添加编辑的护理计划
        // NursingPlan nursingPlan = new NursingPlan();
        // BeanUtils.copyProperties(nursingPlanDto,nursingPlan);
        // nursingPlanMapper.add(nursingPlan);
        // //重新批量添加护理项目
        // if (nursingPlan.getProjectPlans() != null && nursingPlan.getProjectPlans().size() > 0 ){
        // nursingProjectPlanMapper.insertBatch(nursingPlan.getProjectPlans());
        // }
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);
        // 更新护理计划基本信息
        nursingPlanMapper.update(nursingPlan);

        //删除之前护理计划关联的护理项目
        nursingProjectPlanMapper.deleteBuId(nursingPlan);

        //添加最新的 护理计划关联的护理项目
        nursingPlan.getProjectPlans().forEach(nursingProjectPlan -> nursingProjectPlan.setPlanId(nursingPlan.getId()));

        if (nursingPlan.getProjectPlans() != null && nursingPlan.getProjectPlans().size() > 0 ){
            nursingProjectPlanMapper.insertBatch(nursingPlan.getProjectPlans());
        }
    }

    /**
     * 删除护理计划
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        //删除护理计划表
        nursingPlanMapper.deleteById(id);

        //删除护理计划项目相关表
        nursingProjectPlanMapper.deleteById(id);
    }

    /**
     * 启用禁用护理计划
     * @param id
     * @param status
     */
    @Override
    public void enableDisable(Long id, Integer status) {
        NursingPlanVo nursingPlanVo = nursingPlanMapper.getById(id);
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanVo,nursingPlan);
        nursingPlan.setStatus(status);
        nursingPlanMapper.update(nursingPlan);
    }
}
